package cn.com.do4u.ubuntu.jetty;

import cn.com.do4u.ubuntu.IP_Access;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * Created by root on 14-10-26.
 */
public class IP_AccessHandler extends AbstractHandler {

    private Map<Long, String> map = SessionTemp.map;


    @Override
    public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
        String host = httpServletRequest.getRemoteHost();

        httpServletResponse.setContentType("text/html;charset=utf-8");
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        request.setHandled(true);

        PrintWriter out = httpServletResponse.getWriter();
        String cmd = httpServletRequest.getParameter("cmd");
        String rkey = httpServletRequest.getParameter("key");
        String port = httpServletRequest.getParameter("port");
        StringBuffer str = new StringBuffer("");

        if ("key".equals(cmd)) {
            Long key = System.currentTimeMillis();
            map.put(key, key.toString().substring(9));
            str.append(key);
        } else {


            //验证是否存存在key
            if (rkey != null && map.containsValue(rkey)) {
                map.clear();
                str.append("<html><head><title>access ip</title><head><body>");

                String ip = httpServletRequest.getRemoteAddr();
                IP_Access ip_access = new IP_Access(ip);
                //设置开启的端口
                if ("mysql".equals(port)) {
                    ip_access.setPort(3306);
                } else if ("web".equals(port)) {
                    ip_access.setPort(80);
                } else if ("ssl".equals(port)) {
                    ip_access.setPort(443);
                } else if("pptp".equals(port)){
                    ip_access.setPort(1723);
                }else if("ssh".equals(port)){
                    ip_access.setPort(22);
                }else if("8088".equals(port)){
                    ip_access.setPort(8088);
                }

                ip_access.setChain("INPUT");
                if ("add".equals(cmd)) {
                    str.append(ip_access.addTcp().getResult()).append("<br>");
                    str.append(ip_access.addUdp().getResult()).append("<br>");
                } else if ("query".equals(cmd)) {
                    str.append(ip_access.queryInfoBr().getResult()).append("<br>");
                } else if ("del".equals(cmd)) {
                    str.append(ip_access.delAllLines()).append("<br>");

                } else {
                    str.append(ip_access.addTcp().getResult()).append("please input command !<br>");
                }
            } else {
                str.append("fail").append("<br>");
            }
            str.append("</body></html>");
        }
        out.write(str.toString());
        out.flush();
        out.close();
    }
}
